<template>
	<view class="">
		<view class="code_list" v-if="list.length > 0">
			<view class="code_item" v-for="(item,index) in list" :key="index" @click="onCode(item.id)">
				<view class="code_info">
					<view class="code_name">
						{{item.coupon_name}}
					</view>
					<view class="code_num">
						{{item.coupon_price}}
					</view>
				</view>
				<view class="code_time">
					过期时间:{{item.out_time}}
				</view>
				
			</view>
		</view>
		<u-empty v-else icon="http://cdn.uviewui.com/uview/empty/coupon.png" text="暂无优惠券码">
		</u-empty>
		<u-modal :show="show" :title="title" showCancelButton @confirm="dhCode" @cancel="show=false">
			<view class="slot-content">
				<u--input
				    placeholder="请输入优惠码"
				    border="surround"
				    v-model="value"
				  ></u--input>
			</view>
		</u-modal>
		<view class="btn_b">
			<u-button @click="show=true" class="dh_btn" type="default" plain size="large">兑换优惠码</u-button>
		</view>
	</view>
</template>

<script>
	import {exchange,exchangeList} from '@/api/my.js'
	export default {
		data() {
			return {
				show: false,
				title: '兑换优惠码',
				value:'',
				list:[
					// {
					// 	coupon_name:"西洋参黄芪茶（气血茶）300-50",
					// 	coupon_price:'50.00',
					// 	out_time:"2024-12-12 00:00:00"
					// }
				],
				cart_ids:'',
				goods_ids:''
			}
		},
		onLoad(e) {
			console.log(e)
			if(e.cart_ids){
				this.cart_ids = e.cart_ids;
			}
			if(e.goods_ids){
				this.goods_ids = e.goods_ids;
				
			}
			
		},
		mounted(e) {
			
			this.getCodeList()
		},
		methods:{
			onCode(id){
				if(this.cart_ids){
					uni.reLaunch({
						url: "/package/addOrder/index?cart_ids="+this.cart_ids+"&codeId="+id
					});
				}
			},
			dhCode(){
				
				if(!this.value){
					uni.showToast({
						icon:'none',
						title:'请输入优惠码'
					})
					return false
				}
				const data = {
					coupon_code:this.value
				}
				let _this = this;
				exchange(data).then(res=>{
					this.show = false
					uni.showToast({
						icon:'none',
						title:res.msg
					})
					setTimeout(function(){
						this.getCodeList();
					},1000)
					
				}).catch(err=>{
					uni.showToast({
						title:err.data,
						icon:'none'
					})
				})
			},
			getCodeList(){
				let data ={goods_id:''};
				if(this.goods_ids){
					data ={
						goods_id:this.goods_ids
					}
				}
				exchangeList(data).then(res=>{
					this.list = res.data
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: rgb(247, 247, 247);
	}
	.code_list{
		padding: 12px;
		.code_item{
			padding: 12px;
			background-color: #fff;
			border-radius: 10px;
			display: flex;
			flex-direction: column;
			font-size: 14px;
			margin-bottom: 10px;
			.code_info{
				display: flex;
				justify-content: space-between;
				margin-bottom: 12px;
				.code_num{
					font-weight: 700;
				}
			}
			.code_time{
				color: #999;
				font-size: 12px;
			}
		}
	}
	.btn_b {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;

		.dh_btn {
			width: 100%;
		}
	}
</style>